package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    //wx/topic/list
    @Override
    public List<MarketTopic> list() {
        //将limit 设置为10
        PageHelper.startPage(1, 10);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        return marketTopicMapper.selectByExampleSelective(marketTopicExample, MarketTopic.Column.id,
                MarketTopic.Column.title, MarketTopic.Column.subtitle, MarketTopic.Column.price,
                MarketTopic.Column.readCount, MarketTopic.Column.picUrl, MarketTopic.Column.sortOrder,
                MarketTopic.Column.goods, MarketTopic.Column.addTime, MarketTopic.Column.updateTime,
                MarketTopic.Column.deleted, MarketTopic.Column.content);
    }

    //wx/topic/detail
    @Override
    public MarketTopic detail(Integer id) {
        return marketTopicMapper.selectByPrimaryKey(id);
    }

    //wx/topic/related
    @Override
    public PageInfo<MarketTopic> related(Integer id, int page, int limit) {
        //分页
        PageHelper.startPage(page, limit);
        //查询
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        //条件
        marketTopicExample.createCriteria().andDeletedEqualTo(false).andIdNotEqualTo(id);//排除当前专题
        //获取
        List<MarketTopic> list = marketTopicMapper.selectByExampleSelective(marketTopicExample, MarketTopic.Column.id, MarketTopic.Column.title, MarketTopic.Column.subtitle,
                MarketTopic.Column.price, MarketTopic.Column.readCount, MarketTopic.Column.picUrl, MarketTopic.Column.sortOrder, MarketTopic.Column.goods,
                MarketTopic.Column.addTime, MarketTopic.Column.updateTime, MarketTopic.Column.deleted, MarketTopic.Column.content);
        //添加一个不同的专题到专题列表中
        MarketTopic differentTopic = marketTopicMapper.selectByPrimaryKey(2);
        if (differentTopic != null) {
            list.add(differentTopic);
        }
        //返回
        return PageInfo.of(list);
    }
}
